/** 
 
*/
import React, { PureComponent } from 'react';
import { PrimaryC, TableC, primaryGhost } from '@/src/comps';
import { connect } from 'dva';
import SppiTest from './sppiTest';

import config from '@/src/comps/business/tradingMarket/config';
import { message } from 'antd';

class manualJudgment extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      type: 1,
      visible: false, //编辑
      visible2: false, //配置
      exchangeCode: undefined,
      tradingCode: undefined,
      data: {
        pageInfo: {
          currentPage: 1,
          numInPage: 20,
        },
      },
    };
  }

  componentDidMount() {
    // this.getList()
  }

  UNSAFE_componentWillReceiveProps(nextProps) {
    if (JSON.stringify(nextProps.searchData) != JSON.stringify(this.props.searchData)) {
      this.getList({ ...nextProps.searchData, currentPage: 1 });
    }
  }

  getList = (params) => {
    const {
      pageInfo: { currentPage, numInPage },
    } = this.state.data || {};
    this.props.dispatch({
      type: 'sppiTestRecordM/queryByPage',
      payload: {
        currentPage,
        numInPage,
        ...params,
        jrfs: 3,
      },
      callback: (res) => {
        if (res?.data) {
          this.setState({ data: res.data });
        }
      },
    });
  };

  onOk = (e) => {
    this.setState({
      visible: false,
    });
    this.getList({ ...this.props.searchData });
  };

  onCancel = () => {
    this.setState({
      visible: false,
    });
  };
  onEdit = (row) => {
    this.setState({
      visible: true,
      exchangeCode: row.exchangecode,
      tradingCode: row.tradingcode,
      type: 2,
      tenant: row.tenant,
    });
  };

  //表格页码排序改变调用
  handleTableChange = (pagination) => {
    if (this.props.formReset) {
      message.warning('您已重置查询字段，请设置条件后执行查询');
      return;
    }
    const { current, pageSize } = pagination;
    this.getList({
      currentPage: current,
      numInPage: pageSize,
      ...this.props.searchData,
    });
  };

  getColumns = (columns) => {
    if (columns?.length > 0) {
      const newColumns = columns?.map((r) => {
        if (r.key == 'exchangecode') {
          return {
            ...r,
            render: (t) => config.filter((x) => x.value == t)[0]?.title || '--',
          };
        }
        if (r.key == 'sppijg') {
          return {
            ...r,
            render: (t) => (t == '1' ? '通过' : t == '2' ? '不通过' : '--'),
          };
        }
        return r;
      });
      newColumns.push({
        title: '操作',
        dataIndex: 'operate',
        key: 'operate',
        type: 'operate',
        operates: [
          {
            title: '修改',
            onClick: (row) => this.onEdit(row),
          },
        ],
      });
      return newColumns;
    }

    return;
  };

  render() {
    const { visible, visible2, exchangeCode, tradingCode, data = {}, type, tenant } = this.state;
    const { columns = [], dataSource = [], pageInfo } = data;

    return (
      <>
        <div style={{ marginBottom: '20px' }}>
          <PrimaryC title='新增测试' onClick={() => this.setState({ visible: true, type: 1, exchangeCode: undefined, tradingCode: undefined })}></PrimaryC>
          {/* <primaryGhost title='配置' style={{ float: 'right' }} onClick={() => this.setState({ visible2: true })}></primaryGhost> */}
        </div>

        <TableC
          columns={this.getColumns(columns)}
          dataSource={dataSource}
          loading={this.props.loading.effects['sppiTestRecordM/queryByPage']}
          rowKey='id'
          selfScrollY={true}
          current={pageInfo?.currentPage}
          pageSize={pageInfo?.numInPage}
          total={pageInfo?.total || 0}
          onChange={this.handleTableChange}
        ></TableC>

        <SppiTest
          title={type == 1 ? '新增测试' : '编辑测试'}
          visible={visible}
          type={type}
          tenant={type == 1 ? this.props.searchData?.tenantId : tenant}
          exchangeCode={exchangeCode}
          tradingCode={tradingCode}
          onOk={this.onOk}
          onCancel={this.onCancel}
        ></SppiTest>
      </>
    );
  }
}
export default connect(({ sppiTestRecordM, loading }) => ({
  sppiTestRecordM,
  loading,
}))(manualJudgment);
